package com.house.user.controller;

import java.util.Arrays;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.house.common.utils.R;
import com.house.user.entity.UserEntity;
import com.house.user.service.UserService;
import com.house.user.vo.UserLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author yangxinyu
 * @email yangxinyu03201@gmail.com
 * @date 2022-04-22 13:34:09
 */
@RestController
@RequestMapping("user/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 登录
     */
    @RequestMapping("/login")
    public R list(@RequestBody UserLoginVo userLoginVo){
        return userService.login(userLoginVo);
    }

    /**
     * 获取验证码
     */
    @RequestMapping("/getCode/{phone}")
    public R getCode(@PathVariable("phone") String phone){
        return userService.getCode(phone);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{uid}")
    public R info(@PathVariable("uid") Integer uid){
		UserEntity user = userService.getById(uid);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] uids){
		userService.removeByIds(Arrays.asList(uids));

        return R.ok();
    }



    @RequestMapping("/findByName")
    public String findByUserName(@RequestParam("uname") String name){
      LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<>();
      wrapper.eq(UserEntity::getNickname,name);
      UserEntity user = userService.getOne(wrapper);
        return user.getPhone();
    }



}
